package vip.xiaonuo.index.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import vip.xiaonuo.core.util.DateUtils;
import vip.xiaonuo.facedoor.modular.devicebase.entity.DeviceBase;
import vip.xiaonuo.facedoor.modular.devicebase.service.DeviceBaseService;
import vip.xiaonuo.facedoor.modular.devicehistory.entity.DeviceHistory;
import vip.xiaonuo.facedoor.modular.devicehistory.service.DeviceHistoryService;
import vip.xiaonuo.index.vo.CommunityIndexVo;
import vip.xiaonuo.index.vo.TransCount;
import vip.xiaonuo.index.vo.VehicleTransCount;
import vip.xiaonuo.facedoor.modular.visitorhistory.entity.VisitorHistory;
import vip.xiaonuo.facedoor.modular.visitorhistory.service.VisitorHistoryService;
import vip.xiaonuo.modular.securitydata.entity.SecurityData;
import vip.xiaonuo.modular.securitydata.service.SecurityDataService;
import vip.xiaonuo.vehicle.modular.vehicletrafficrecord.entity.VehicleTrafficRecord;
import vip.xiaonuo.vehicle.modular.vehicletrafficrecord.service.VehicleTrafficRecordService;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommunityIndexService {

    @Resource
    private DeviceBaseService deviceBaseService;

    @Resource
    private DeviceHistoryService deviceHistoryService;

    @Resource
    private VisitorHistoryService visitorHistoryService;

    @Resource
    private SecurityDataService securityDataService;

    @Resource
    private VehicleTrafficRecordService vehicleTrafficRecordService;

    public CommunityIndexVo index() {

        Date dayBegin = DateUtils.getDayBegin();
        Date dayEnd = DateUtils.getDayEnd();

        CommunityIndexVo indexVo = new CommunityIndexVo();

        // 全部设备
        long allDeviceNum = deviceBaseService.count();
        indexVo.setDeviceNum(allDeviceNum);

        // 设备
        long onLine = deviceBaseService.count(new LambdaQueryWrapper<DeviceBase>().eq(DeviceBase::getDeviceState, 1));
        indexVo.setOnLineDeviceNum(onLine);
        indexVo.setOfflineDeviceNum(allDeviceNum - onLine);

        // 内部人员通行数据
        long memberTransNum = deviceHistoryService.count(new LambdaQueryWrapper<DeviceHistory>()
                .between(DeviceHistory::getDeviceCurrentDate, dayBegin, dayEnd));
        indexVo.setMemberTransNum(memberTransNum);
        // 访客通行数据
        long visitorTransNum = visitorHistoryService.count(new LambdaQueryWrapper<VisitorHistory>()
                .between(VisitorHistory::getDeviceCurrentDate, dayBegin, dayEnd));
        indexVo.setVisitTransNum(visitorTransNum);
        // 总计通行数量
        indexVo.setTransNum(memberTransNum + visitorTransNum);

        // 人员识别数据
        long captureNum = securityDataService.count(new LambdaQueryWrapper<SecurityData>()
                .between(SecurityData::getCaptureTime, dayBegin, dayEnd));
        indexVo.setCaptureNum(captureNum);

        long memberCaptureNum = securityDataService.count(new LambdaQueryWrapper<SecurityData>()
                .eq(SecurityData :: getPersonType ,"员工")
                .between(SecurityData::getCaptureTime, dayBegin, dayEnd));
        indexVo.setMemberCaptureNum(memberCaptureNum);
        indexVo.setStrangerCaptureNum(captureNum - memberCaptureNum);

        // 人员通行每日汇总
        indexVo.setTransData(this.getTransData(dayBegin,dayEnd,10));

        // 车辆通行每日汇总
        indexVo.setVehicleTransData(this.getVehicleTransData(dayBegin,dayEnd,10));

        return indexVo;
    }

    private List<TransCount> getTransData(Date dayBegin, Date dayEnd, int index) {
        List<TransCount> result = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
        dayBegin = DateUtils.addDay(dayBegin,-index);
        dayEnd = DateUtils.addDay(dayEnd,-index);
        for(int i = 0 ; i < index ; i++){
            TransCount count = new TransCount();
            // 内部人员通行数据
            long memberTransNum = deviceHistoryService.count(new LambdaQueryWrapper<DeviceHistory>()
                    .between(DeviceHistory::getDeviceCurrentDate, dayBegin, dayEnd));
            count.setMemberTransNum(memberTransNum);
            // 访客通行数据
            long visitorTransNum = visitorHistoryService.count(new LambdaQueryWrapper<VisitorHistory>()
                    .between(VisitorHistory::getDeviceCurrentDate, dayBegin, dayEnd));
            count.setVisitTransNum(visitorTransNum);

            count.setDate(sdf.format(dayBegin));
            result.add(count);

            dayBegin = DateUtils.addDay(dayBegin,1);
            dayEnd = DateUtils.addDay(dayEnd,1);
        }
        return result;
    }

    private List<VehicleTransCount> getVehicleTransData(Date dayBegin, Date dayEnd, int index) {
        List<VehicleTransCount> result = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
        dayBegin = DateUtils.addDay(dayBegin,-index);
        dayEnd = DateUtils.addDay(dayEnd,-index);
        for(int i = 0 ; i < index ; i++){
            VehicleTransCount count = new VehicleTransCount();
            // 进 数据
            long inNum = vehicleTrafficRecordService.count(new LambdaQueryWrapper<VehicleTrafficRecord>()
                    .between(VehicleTrafficRecord::getTrafficTime, dayBegin, dayEnd)
                    .eq(VehicleTrafficRecord :: getInOut , 1));
            count.setInNum(inNum);
            // 出 数据
            long outNum = vehicleTrafficRecordService.count(new LambdaQueryWrapper<VehicleTrafficRecord>()
                    .between(VehicleTrafficRecord::getTrafficTime, dayBegin, dayEnd)
                    .eq(VehicleTrafficRecord :: getInOut , 2));
            count.setOutNum(outNum);

            count.setDate(sdf.format(dayBegin));
            result.add(count);

            dayBegin = DateUtils.addDay(dayBegin,1);
            dayEnd = DateUtils.addDay(dayEnd,1);
        }
        return result;
    }


}
